package com.spa.interfaces.shop;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.spa.application.command.Login;
import com.spa.application.dto.login.LoginDTO;
import com.spa.application.query.member.LoginQuery;
import com.spa.application.query.member.SmsQuery;
import com.spa.application.service.shop.ShopLoginApplicationService;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.infrastructure.enums.dict.base.RequestSourceEnum;
import com.spa.interfaces.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/shop/login")
public class ShopLoginController extends BaseController {

    @Resource
    private ShopLoginApplicationService shopLoginApplicationService;

    @PostMapping
    @SaIgnore
    public ResponseMessage<LoginDTO> shopLogin(@RequestBody LoginQuery query) {
        Login login = new Login();
        BeanUtil.copyProperties(query, login);
        return responseHelper.success(shopLoginApplicationService.smsLogin(login, RequestSourceEnum.Shop));
    }

    @PostMapping("/outLogin")
    public ResponseMessage outLogin() {
        StpUtil.logout();
        return responseHelper.success();
    }

    @PostMapping("/sendSms")
    @SaIgnore
    public ResponseMessage sendSms(@Validated @RequestBody SmsQuery query) {
        shopLoginApplicationService.sendSms(query.getPhone());
        return responseHelper.success();
    }
}
